<script setup>
import { useRouter } from 'vue-router'; 
import { inject, ref } from "vue";
import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
const data = inject('globalData');
const parentRouter = inject('parentRouter');
const setGlobalState = inject('setGlobalState')
const router = useRouter();

const num = ref(Math.random())
function goPage() {
  router.push({
    path: '/vue1/page2'
  })
}

function goPage2() {
  location.href = '/team2/#/vue2/page1'
  // parentRouter.push({
  //   path: '/team2/#/vue2/page1'
  // })
}

function goPage3() {
  parentRouter.push({
    path: '/base'
  })
}

function handleSendData() {
  console.info('window.qiankunStarted', window.qiankunStarted)
  if (qiankunWindow.__POWERED_BY_QIANKUN__) {
    num.value = Math.random();
    setGlobalState({
      data: {
        num
      }
    });
  } else {
    console.error("请在qiankun环境下使用");
  }
};

</script>

<template>
  <div>子应用vue1下级页面1</div>
  <button @click="goPage()">跳转页面team1/vue1/page2</button>
  <button @click="goPage2()">跳转页面team2/#/vue2/page1</button>
  <button @click="goPage3()">跳转页面/base</button>
  <button @click="handleSendData()">向父级传值{{ num }}</button>
  <div>{{ data.age }}</div>
</template>

<style scoped>
  button {
    margin: 20px;
    padding: 10px;
    border-radius: 8px;
  }
</style>
